﻿using System;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Coffee_Restaurant.Database;
using Coffee_Restaurant.UserControls;
using MetroFramework.Controls;

namespace Coffee_Restaurant.Forms
{
    public partial class DisplayListTable : Form
    {
        private MetroTile metroTile1;
        public DisplayListTable()
        {
            InitializeComponent();
        }

        private void DisplayListTable_Load(object sender, EventArgs e)
        {
            LoadDataArea();
        }

        public void LoadDataArea()
        {
            panelContainer.Controls.Clear();
            using (var db = new DatabaseEntities())
            {
                int sizeTableNumber = Convert.ToInt32(ConfigurationManager.AppSettings["SizeTableNumber"]);

                //select list khu vuc
                //OrderByDescending(x => x.AreaId)
                var model = db.sp_AreasDataSoure().OrderByDescending(x => x.AreaId).ToList();
                foreach (var area in model)
                {
                    //khoi tao userControl Display khu vuc ban
                    var displayAreaTable = new DisplayAreaTable();
                    displayAreaTable.Dock = DockStyle.Top;
                    displayAreaTable.lblNameArea.Text = area.Name + " (" + area.CountTable + ")";
                    var modelTable = db.Tables.Where(t => t.AreaId == area.AreaId).ToList();
                    #region set height auto flow item
                    //cach tinh so dong
                    //moi dong co 5 item
                    //moi item height 60 + 20 padding = 80
                    //total item / so item cua? dong + them 2 dong nua
                    int columnTable = Convert.ToInt32(ConfigurationManager.AppSettings["ColumnTable"]);
                    var line = (modelTable.Count / columnTable) + 2; // tinh xem bao nhieu hang
                    displayAreaTable.Height = line * (sizeTableNumber + 10); // nhan chieu cao theo so hang
                    #endregion

                    //add table vao usercontrol
                    foreach (var table in modelTable)
                    {
                        displayAreaTable.flowLayoutPanel1.Controls.Add(AddTableNumber(table.Name));
                    }
                    panelContainer.Controls.Add(displayAreaTable);
                }

            }
        }
        public Button AddTableNumber(string name)
        {
            //_buttonTableNumber = new Button();
            //_buttonTableNumber.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.table;
            //_buttonTableNumber.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            //_buttonTableNumber.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            //_buttonTableNumber.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            //_buttonTableNumber.FlatAppearance.BorderSize = 0;
            //_buttonTableNumber.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            //_buttonTableNumber.Font = new System.Drawing.Font("Camlam", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            //_buttonTableNumber.ForeColor = System.Drawing.Color.White;
            //_buttonTableNumber.Location = new System.Drawing.Point(184, 248);
            //_buttonTableNumber.Name = "btntableNumber";
            //_buttonTableNumber.Size = new System.Drawing.Size(80, 80);
            //_buttonTableNumber.Margin = new Padding(10);
            //_buttonTableNumber.TabIndex = 0;
            //_buttonTableNumber.Text = name;
            //_buttonTableNumber.UseVisualStyleBackColor = true;
            //_buttonTableNumber.Cursor = System.Windows.Forms.Cursors.Hand;
            //_buttonTableNumber.Enter += _buttonTableNumber_Enter;
            //_buttonTableNumber.MouseHover += btn_MouseHover;
            //_buttonTableNumber.MouseLeave += btn_MouseLeave;

            // metroTile1
            // 
            this.metroTile1 = new MetroFramework.Controls.MetroTile();
            this.metroTile1.ActiveControl = null;
            this.metroTile1.Location = new System.Drawing.Point(11, 37);
            this.metroTile1.Name = "metroTile1";
            this.metroTile1.Size = new System.Drawing.Size(60, 60);
            this.metroTile1.TabIndex = 2;
            this.metroTile1.Text = "Bàn";
            this.metroTile1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.metroTile1.TileCount = 1;
            metroToolTip1.SetToolTip(this.metroTile1, "Bàn số 1");
            this.metroTile1.UseSelectable = true;
            return metroTile1;
        }

        /// <summary>
        /// fix loi tu dong select table ban hang
        /// chua tim ra nguyen nhan tu dong select nen set color = white trung voi mau nen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _buttonTableNumber_Enter(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
                btn.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
                btn.FlatAppearance.BorderSize = 0;
                btn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                btn.FlatAppearance.BorderColor = Color.FromArgb(0, 226, 211, 190);
            }
        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.table_hover;
            }
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                btn.BackgroundImage = global::Coffee_Restaurant.Properties.Resources.table;
            }
        }
    }
}
